//
//  SATopUpScreeningView.m
//  saasRetail
//
//  Created by mac on 2023/3/19.
//

#import "SATopUpScreeningView.h"
#import "SATopUpScreeningCell.h"

@interface SATopUpScreeningView ()<UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITextField *searchTf;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation SATopUpScreeningView

+(instancetype)shareCornView {
    SATopUpScreeningView * shareView = [[SATopUpScreeningView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    return shareView;
}

-(instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = kColor(0, 0, 0, 0.6);
        [self creatUI];
    }
    return self;
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SATopUpScreeningCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SATopUpScreeningCell" forIndexPath:indexPath];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(40);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(void)creatUI {
    
    UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(kScaleNum(60), 0, kScaleNum(315), kScreenHeight)];
    backGroundView.backgroundColor = [UIColor whiteColor];
    backGroundView.userInteractionEnabled = YES;
    [self addSubview:backGroundView];
    
    [backGroundView addSubview:self.searchTf];
    
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame = SARectMake(240, StatusHeight - 44 + 7, 75, 30);
    [deleteBtn setTitle:@"取消" forState:UIControlStateNormal];
    [deleteBtn setTitleColor:kGrayColor forState:UIControlStateNormal];
    deleteBtn.titleLabel.font = kFont(15);
    [deleteBtn addTarget:self action:@selector(shareDissTapEvent) forControlEvents:UIControlEventTouchUpInside];
    [backGroundView addSubview:deleteBtn];
    
    [backGroundView addSubview:self.tableView];
}

- (void)showView:(UIViewController *)Sview {
    if(Sview==nil){
        [[[UIApplication sharedApplication].windows lastObject] addSubview:self];
    }else{
        [Sview.view addSubview:self];
    }
}

-(void)shareDissTapEvent {
    [UIView animateWithDuration:0.5f animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(UITextField *)searchTf {
    if (!_searchTf) {
        _searchTf = [[UITextField alloc] initWithFrame:SARectMake(20, StatusHeight - 44 + 7, 220, 30)];
        _searchTf.placeholder = @"输入充值店员名称/手机号码";
        _searchTf.backgroundColor = kBgColor;
        _searchTf.layer.cornerRadius = kScaleNum(5);
        _searchTf.layer.masksToBounds = YES;
        _searchTf.delegate = self;
        _searchTf.font = kFont(14);
        _searchTf.clearButtonMode = UITextFieldViewModeWhileEditing;
        UIView *leftView = [[UIView alloc] initWithFrame:SARectMake(0, 0, 26, 30)];
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:SARectMake(6, 7, 14, 16)];
        imageV.image = [UIImage imageNamed:@"shop_search"];
        [leftView addSubview:imageV];
        _searchTf.leftView = leftView;
        _searchTf.leftViewMode = UITextFieldViewModeAlways;
        [_searchTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _searchTf;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight), kScaleNum(315), kScreenHeight - kScaleNum(StatusHeight)) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kColor(246, 246, 246, 1);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SATopUpScreeningCell class] forCellReuseIdentifier:@"SATopUpScreeningCell"];
    }
    return _tableView;
}


@end
